using Auth.Domain.Entities;
using SqlSugar;
using System.Linq.Expressions;

namespace Auth.Infrastructure.Repositories
{
    /// <summary>
    /// 认证仓储实现
    /// </summary>
    /// <typeparam name="T">实体类型</typeparam>
    public class AuthRepository<T> : IAuthRepository<T> where T : BaseEntity, new()
    {
        private readonly ISqlSugarClient _db;

        public AuthRepository(ISqlSugarClient db)
        {
            _db = db;
        }

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity">要添加的实体</param>
        /// <returns>影响的行数</returns>
        public int Create(T entity)
        {
            if (entity == null)
                throw new ArgumentNullException(nameof(entity));

            try
            {
                return _db.Insertable(entity).ExecuteCommand();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error creating entity: {ex.Message}");
                throw;
            }
        }

        /// <summary>
        /// 获取所有实体
        /// </summary>
        /// <returns>实体列表</returns>
        public List<T> GetAll()
        {
            try
            {
                return _db.Queryable<T>().Where(e => !e.IsDeleted).ToList();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error getting all entities: {ex.Message}");
                throw;
            }
        }

        /// <summary>
        /// 根据条件查询实体
        /// </summary>
        /// <param name="whereExpression">查询条件</param>
        /// <returns>实体列表</returns>
        public List<T> GetByCondition(Func<T, bool> whereExpression)
        {
            try
            {
                return _db.Queryable<T>()
                    .Where(e => !e.IsDeleted)
                    .ToList()
                    .Where(whereExpression)
                    .ToList();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error getting entities by condition: {ex.Message}");
                throw;
            }
        }

        /// <summary>
        /// 通过ID获取实体
        /// </summary>
        /// <param name="id">实体的唯一标识</param>
        /// <returns>实体对象，如果未找到则返回null</returns>
        public T GetById(long id)
        {
            if (id <= 0)
                throw new ArgumentOutOfRangeException(nameof(id), "ID 必须大于 0");

            try
            {
                return _db.Queryable<T>().InSingle(id);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error getting entity by ID {id}: {ex.Message}");
                throw;
            }
        }

        /// <summary>
        /// 通过用户名获取用户
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns>用户实体，如果未找到则返回null</returns>
        public UserModel GetUserByUserName(string userName)
        {
            if (string.IsNullOrEmpty(userName))
                throw new ArgumentNullException(nameof(userName));

            try
            {
                return _db.Queryable<UserModel>()
                    .Where(u => u.UserName == userName && !u.IsDeleted)
                    .First();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error getting user by username {userName}: {ex.Message}");
                throw;
            }
        }

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity">要更新的实体</param>
        /// <returns>影响的行数</returns>
        public int Update(T entity)
        {
            if (entity == null)
                throw new ArgumentNullException(nameof(entity));

            try
            {
                return _db.Updateable(entity).ExecuteCommand();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error updating entity: {ex.Message}");
                throw;
            }
        }
    }
} 